package com.umfb.timertask;


import com.umfb.config.zk.ZooKeeperPropertyPlaceholderConfigurer;
import com.umfb.utils.UmfbApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableAutoConfiguration
@EnableScheduling
public class DubboDemoTimertaskApplication extends WebMvcConfigurerAdapter {
	private static  final Logger logger = LoggerFactory.getLogger(DubboDemoTimertaskApplication.class);
	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication (DubboDemoTimertaskApplication.class);
		final ApplicationContext ac = springApplication.run(DubboDemoTimertaskApplication.class,args);
		UmfbApplicationContext.setApplicationContext(ac);

		logger.info("-------------------Dubbo timertask service started----------------------");
	}

	@Bean
	public TaskScheduler taskScheduler(){
		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
		//线程池大小
		taskScheduler.setPoolSize(10);
		//线程名字的前缀
		taskScheduler.setThreadNamePrefix("springBoot-task");

		return taskScheduler;
	}

	@Bean
	public UmfbApplicationContext initAppEnd(ApplicationContext ac) {
		UmfbApplicationContext.setApplicationContext(ac);

		return new UmfbApplicationContext();

	}

	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	@Bean
	public PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {

		PropertySourcesPlaceholderConfigurer configurer = new ZooKeeperPropertyPlaceholderConfigurer();
		configurer.setIgnoreResourceNotFound(true);
		configurer.setLocations(resourcePatternResolver.getResource("classpath:application.properties"));

		return configurer;
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedOrigins("*");
	}
}
